package  com.example.cursor_animation
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler
import com.intellij.openapi.editor.colors.EditorColors
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.colors.EditorColorsScheme
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.ui.popup.PopupStep
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
import com.intellij.ui.JBColor
import com.sun.istack.Nullable
import java.awt.Color
import java.util.*
import kotlin.random.Random

fun getCurrentEditor(project: Project): Editor? {
    val fileEditorManager = FileEditorManager.getInstance(project)
    return fileEditorManager.selectedTextEditor
}
fun changeCursorColor(project: Project, color: Color) {
    val   editor = getCurrentEditor(project) ?: return
    val editorColorsManager = EditorColorsManager.getInstance()
    val globalScheme = editorColorsManager.globalScheme.clone() as EditorColorsScheme

    // 创建新的文本属性并设置光标颜色
    val textAttributes = globalScheme.getAttributes(EditorColors.CODE_LENS_BORDER_COLOR).clone() as TextAttributes
    textAttributes.foregroundColor = color

    // 设置光标颜色
    globalScheme.setColor(EditorColors.CARET_COLOR, color)
    editor.colorsScheme.setColor(EditorColors.CARET_COLOR,color )// 将修改后的颜色方案应用到编辑器
}

class CursorColorUtil {
    fun getCurrentEditor(project: Project): Editor? {
        val fileEditorManager = FileEditorManager.getInstance(project)
        return fileEditorManager.selectedTextEditor
    }

    fun changeCursorColor(project: Project, color: Color) {
        val editor = getCurrentEditor(project) ?: return
        val editorColorsManager = EditorColorsManager.getInstance()
        val globalScheme = editorColorsManager.globalScheme.clone() as EditorColorsScheme

        // 创建新的文本属性并设置光标颜色
        val textAttributes = globalScheme.getAttributes(EditorColors.CODE_LENS_BORDER_COLOR).clone() as TextAttributes
        textAttributes.foregroundColor = color

        // 设置光标颜色
        globalScheme.setColor(EditorColors.CARET_COLOR, color)
        editor.colorsScheme.setColor(EditorColors.CARET_COLOR , color ) // 将修改后的颜色方案应用到编辑器
    }
}

class cursorViews : AnAction()  {
    override fun actionPerformed(event: AnActionEvent ) {
        val project: Project = event.project ?: return
        val cursorColorUtil = CursorColorUtil()
        //可序列化 代表可以被转化为字节流存储
        val    menuList: MutableList<Color> = mutableListOf(
            JBColor.GREEN, JBColor.MAGENTA ,JBColor.CYAN, JBColor.LIGHT_GRAY , JBColor.DARK_GRAY , JBColor.red,
            JBColor.pink, JBColor.ORANGE, JBColor.YELLOW, JBColor.blue) ;
        // 修改光标颜色为蓝色
        val length =menuList.size;
        val  randomNumber = Random.nextInt(0,length)
        cursorColorUtil.changeCursorColor(project,  menuList[randomNumber])
    }
}
 class  startActivityCursorColor : ProjectActivity{
    override suspend fun execute(project: Project) {
        val cursorColorUtil = CursorColorUtil()
        // 修改光标颜色为蓝色
        cursorColorUtil.changeCursorColor(project, JBColor.ORANGE)
    }
}

class ChildPopupMenuToChangeCursorColor
protected  constructor() : EditorAction(Handler()) {
    private class Handler : EditorWriteActionHandler() {
        @Deprecated("Deprecated in Java")
        override fun executeWriteAction(@Nullable editor: Editor, @Nullable dataContext: DataContext) {

            val project = editor.project ?: return

//            val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return
//
//            val selction = editor.selectionModel.getSelectedText()?:return ;
            val menuItems: List<String> = listOf("GREEN", "MAGENTA", "CYAN", "LIGHT_GRAY", "DARK_GRAY", "RED"
            , "PINK" , "ORANGE" , "YELLOW" ,"BLUE")
            val iterator: Iterator<String> = menuItems.iterator()
            val popup = JBPopupFactory.getInstance()
                .createListPopup(object : BaseListPopupStep<String>("改变光标颜色", menuItems) {
                    override fun onChosen(selectedValue: String?, finalChoice: Boolean): PopupStep<*>? {
                        when(selectedValue ) {
                               "GREEN" ->  {
                                   return doFinalStep {
                                       val cursorColorUtil = CursorColorUtil()
                                       cursorColorUtil.changeCursorColor(project, JBColor.GREEN)
                                   }
                               }
                             "MAGENTA" -> {
                                 return doFinalStep {
                                     val cursorColorUtil = CursorColorUtil()
                                     cursorColorUtil.changeCursorColor(project, JBColor.MAGENTA)
                                 }
                             }
                            "CYAN" -> {
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.CYAN)
                                }
                            }
                            "LIGHT_GRAY"  ->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.LIGHT_GRAY)
                                }
                            }
                            "DARK_GRAY" ->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.DARK_GRAY)
                                }
                            }
                            "RED" ->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.red)
                                }
                            }
                            "PINK" -> {
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.pink)
                                }
                            }
                            "ORANGE" ->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.ORANGE)
                                }
                            }
                            "YELLOW" ->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.YELLOW)
                                }
                            }
                            "BLUE"->{
                                return doFinalStep {
                                    val cursorColorUtil = CursorColorUtil()
                                    cursorColorUtil.changeCursorColor(project, JBColor.blue)
                                }
                            }
                        }
//                        return doFinalStep {
//                            JOptionPane.showMessageDialog(null, "Selected: $selectedValue")
//                        }
                        return super.onChosen(selectedValue, finalChoice)
                    }
                })

            popup.showInBestPositionFor(editor)
        }
    }
}